<template>
  <div v-loading="!singers.length" class="singer">
    <IndexList :data="singers"
               @select="selectSinger">
    </IndexList>
    <router-view v-slot="{ Component }">
      <transition appear name="slide">
        <component :is="Component" :singer="selectedSinger"></component>
      </transition>
    </router-view>
  </div>
</template>

<script>
import API from '../api/singer'
import IndexList from '../components/index-list/IndexList'
import storage from 'good-storage'
import { SINGER_KEY } from '../assets/js/constant'

export default {
  name: 'singer',
  components: { IndexList },
  data () {
    return {
      singers: [],
      selectedSinger: null
    }
  },
  async created () {
    const result = await API.getSingerList()
    this.singers = result.singers
    // console.log(result)
    console.log(this.singers)
  },
  methods: {
    selectSinger (singer) {
      this.selectedSinger = singer

      // 将歌曲信息设置到缓存中
      this.cacheSinger(singer)
      this.$router.push({
        path: `/singer/${singer.mid}`
      })
    },
    cacheSinger (singer) {
      storage.session.set(SINGER_KEY, singer)
    }
  }

  /*setup () {
    const singerList = ref(null)

    singerAPI.getSingerList()
      .then((res) => {
        console.log(res)
        // bug点: TypeError: undefined is not iterable (cannot read property Symbol(Symbol.iterator))
        // singerList.push([...res.data])
        singerList.value = res
        console.log(singerList)
        console.log(singerList.value)
      }).catch((err) => {
      console.log(err)
    })
    return {
      singerList
    }
  }*/
}
</script>

<style lang="scss" scoped>
.singer {
  // 实现滚动的关键步骤
  position: fixed;
  top: 88px;
  bottom: 0;
  width: 100%;
}
</style>
